package net.atomarrow.controllers.converter;

import java.util.Date;

import net.atomarrow.util.DateUtil;
import net.atomarrow.util.StringUtil;

public class DateConverter implements IConverter {

    @Override
    public boolean canHandle(Class clazz) {
        return clazz == Date.class;
    }

    @Override
    public <T> T converter(Object[] values, Class<T> clazz) throws Exception{
        Object value =values[0];
        if(value==null||StringUtil.isBlank(value.toString())){
            return null;
        }
        Date date;
        if(value instanceof Date){
        	date = (Date) value;
        }else{
        	date =  DateUtil.getDate(value.toString());
        }
        if(date==null){
            throw new RuntimeException("can not convert "+ value);
        }
        return (T)date;
    }
}
